package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.JqbzSettings;
import com.jy.adscm.wechat.bean.WechatMaterialUninstall;
import com.jy.adscm.wechat.bean.WechatUser;
import com.jy.adscm.wechat.mapper.mysql.JqbzSettingsMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatMaterialUninstallMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserMapper;
import com.jy.adscm.wechat.service.WechatMaterialUninstallService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-10-09
 */
@Service
public class WechatMaterialUninstallServiceImpl extends ServiceImpl<WechatMaterialUninstallMapper, WechatMaterialUninstall> implements WechatMaterialUninstallService {
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;
    @Override
    public Boolean insertWechatMaterialUninstall(WechatMaterialUninstall wechatMaterialUninstall, String openid) {
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
        wechatMaterialUninstall.setCreateTime(new Date());
        wechatMaterialUninstall.setCreator(wechatUser.getId().toString());
        boolean insert = super.insert(wechatMaterialUninstall);
        return insert;
    }

    @Override
    public List<WechatMaterialUninstall> selectByPid(Long pid, Integer type) {

        Wrapper<WechatMaterialUninstall> wrapper =new EntityWrapper<>();
        wrapper.eq("pid",pid);
        wrapper.eq("type",type);
        List<WechatMaterialUninstall> wechatMaterialUninstalls = super.selectList(wrapper);
        for (WechatMaterialUninstall wechatMaterialUninstall : wechatMaterialUninstalls) {
         if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterialUninstall.getParts(),null)){
             JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(wechatMaterialUninstall.getParts());
             wechatMaterialUninstall.setJqbzSettings(jqbzSettings);
         }
         if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterialUninstall.getCreator(),null)){
             WechatUser wechatUser = wechatUserMapper.selectById(wechatMaterialUninstall.getCreator());
             wechatMaterialUninstall.setCreatorName(wechatUser.getName());
         }
        }
        return wechatMaterialUninstalls;
    }
}
